//
//  ActivityIndicatorView.m
//  BuyDesire
//
//  Created by Hung.Nguyen on 9/15/12.
//  Copyright (c) 2012 BuyDesire. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "ActivityIndicatorView.h"

@implementation ActivityIndicatorView

@synthesize padding=_padding;

static CGFloat BORDER_WIDTH = 10;

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- (id)initWithHorizontalActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
    _isFullscreen = FALSE;
    isHorizontal = YES;
    if (self = [super initWithFrame:CGRectZero])
	{
		self.padding = 5;
		self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
		self.layer.cornerRadius = 5.0f;
		self.layer.borderWidth = 2;
		self.layer.borderColor = [UIColor clearColor].CGColor;
		
		_indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
		self.frame = CGRectMake(0,0,_indicatorView.bounds.size.width + 2*self.padding,_indicatorView.bounds.size.height + 2*self.padding);
        CGRect iFrame = _indicatorView.bounds;
		iFrame.origin.x += 5;
		iFrame.origin.y += self.padding;
		_indicatorView.frame = iFrame;
		_indicatorView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin
		|UIViewAutoresizingFlexibleTopMargin
		|UIViewAutoresizingFlexibleLeftMargin
		|UIViewAutoresizingFlexibleRightMargin;
		[self addSubview:_indicatorView];
	}
    return self;
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- (id)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
    return [self initWithActivityIndicatorStyle:style onFullScreen: FALSE];
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- (id)initWithFrame:(CGRect) frame activityIndicatorStyle:(UIActivityIndicatorViewStyle)style
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
    _isFullscreen = FALSE;
    if (self = [super initWithFrame:frame])
	{
		self.padding = 5;
		self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
		self.layer.cornerRadius = 10.0f;
		self.layer.borderWidth = BORDER_WIDTH;
		self.layer.borderColor = [UIColor clearColor].CGColor;
		
		_indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
		self.frame = frame;
		CGRect iFrame = _indicatorView.bounds;
		iFrame.origin.x += 50;
		iFrame.origin.y += self.padding;
		_indicatorView.frame = iFrame;
		_indicatorView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin
		|UIViewAutoresizingFlexibleTopMargin
		|UIViewAutoresizingFlexibleLeftMargin
		|UIViewAutoresizingFlexibleRightMargin;
		[self addSubview:_indicatorView];
	}
    return self;
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
- (id)initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyle)style onFullScreen: (BOOL) isFullScreen
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
	_isFullscreen = isFullScreen;
    if (self = [super initWithFrame:CGRectZero])
	{
		self.padding = 16;
		self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.6];
        //self.backgroundColor = [UIColor colorWithRed:2.0/255.0 green:14.0/255.0 blue:55.0/255.0 alpha:1.0];
        //self.backgroundColor = [UIColor colorWithRed:146.0/255.0 green:52.0/255.0 blue:53.0/255.0 alpha:1.0];
		self.layer.cornerRadius = 10.0f;
		self.layer.borderWidth = 10;
		self.layer.borderColor = [UIColor clearColor].CGColor;
		
		_indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
		self.frame = CGRectMake(0,0,_indicatorView.bounds.size.width + 2*self.padding,_indicatorView.bounds.size.height + 2*self.padding);
		CGRect iFrame = _indicatorView.bounds;
		iFrame.origin.x += self.padding;
		iFrame.origin.y += self.padding;
		_indicatorView.frame = iFrame;
		_indicatorView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin
        |UIViewAutoresizingFlexibleTopMargin
        |UIViewAutoresizingFlexibleLeftMargin
        |UIViewAutoresizingFlexibleRightMargin;
		[self addSubview:_indicatorView];
        
        
        // Set the colors for the gradient layer.
//        static NSMutableArray *colors = nil;
//        if (colors == nil) {
//            colors = [[NSMutableArray alloc] initWithCapacity:3];
//            UIColor *color = nil;
//            color = [UIColor colorWithRed:0.82 green:0.84 blue:0.87 alpha:1.0];
//            [colors addObject:(id)[color CGColor]];
//            color = [UIColor colorWithRed:0.41 green:0.41 blue:0.59 alpha:1.0];
//            [colors addObject:(id)[color CGColor]];
//            color = [UIColor colorWithRed:0.41 green:0.41 blue:0.59 alpha:1.0];
//            [colors addObject:(id)[color CGColor]];
//        }
//        [(CAGradientLayer *)self.layer setColors:colors];
//        [(CAGradientLayer *)self.layer setLocations:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.48], [NSNumber numberWithFloat:1.0], nil]];
        
	}
    return self;
}

+ (Class)layerClass {
    
    return [CAGradientLayer class];
}

-(void) setText: (NSString*) text
{
    if (text.length > 0)
	{
		if (!_label) {
            if (isHorizontal) {
                UIFont *font = [UIFont fontWithName:@"Georgia" size:15.0];
                CGSize textSize = [text sizeWithFont:font];
                
                self.frame = CGRectMake(0,0, 20 + textSize.width + 3*self.padding + _indicatorView.bounds.size.width, _indicatorView.bounds.size.height + 2*self.padding);
                CGRect iFrame = _indicatorView.bounds;
                iFrame.origin.x = self.padding;
                iFrame.origin.y = self.padding;
                _indicatorView.frame = iFrame;
                
                CGRect f = self.bounds;
                
                f.size.height = 30;
                CGFloat xOffset = 0;
                CGFloat yOffset = 0;
                xOffset = 2*self.padding + _indicatorView.frame.size.width;
                yOffset = self.padding + (_indicatorView.frame.size.height - f.size.height) * 0.5f;
                
                f.origin.y = yOffset;
                f.origin.x = xOffset;
                
                
                _label = [[UILabel alloc] initWithFrame: f];
                _label.font = font;
                
                _label.textColor = [UIColor whiteColor];
                _label.shadowColor = [UIColor grayColor];
                _label.shadowOffset = CGSizeMake(0,1);
                _label.backgroundColor = [UIColor clearColor];
                _label.textAlignment = UITextAlignmentLeft;
                _label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
                _label.numberOfLines = 2;
                
                [self addSubview:_label];
            } else {
                CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:15.0]];
                
                self.frame = CGRectMake(0,0,textSize.width + 2*self.padding, _indicatorView.bounds.size.height + 2*self.padding);
                CGRect iFrame = _indicatorView.bounds;
                iFrame.origin.x = (self.frame.size.width - iFrame.size.width)/2;
                iFrame.origin.y = (self.frame.size.height - iFrame.size.height - textSize.height)/2;
                _indicatorView.frame = iFrame;
                
                CGRect f = self.bounds;
                
                f.size.height = 30;
                CGFloat xOffset = 0;
                CGFloat yOffset = 0;
                xOffset = 3;
                yOffset = self.padding + _indicatorView.frame.size.height;
                
                f.origin.y = yOffset;
                if (_isFullscreen) {
                    f.origin.y = _indicatorView.frame.origin.y + 5;
                }
                
                f.origin.x = xOffset;
                if (_isFullscreen) {
                    f.origin.x = self.padding - 8;
                }
                f.size.width -= 6;
                _label = [[UILabel alloc] initWithFrame: f];
                _label.font = [UIFont systemFontOfSize:15];
                
                _label.textColor = [UIColor whiteColor];
                _label.shadowColor = [UIColor grayColor];
                _label.shadowOffset = CGSizeMake(0,1);
                _label.backgroundColor = [UIColor clearColor];
                _label.textAlignment = UITextAlignmentCenter;
                _label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
                if (_isFullscreen) {
                    _label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
                }
                _label.numberOfLines = 2;
                f = self.frame;
                f.size.height += 30 + 6 - self.padding;
                
                if (!_isFullscreen) {
                    self.frame = f;
                }
                [self addSubview:_label];
            }
        }
        
        _label.text = text;
    } else {
		[_label removeFromSuperview];
		_label = nil;
		[self setNeedsLayout];
	}
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
-(void) startAnimating
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
	[_indicatorView startAnimating];
	self.hidden = NO;
}

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
-(void) stopAnimating
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
{
	[_indicatorView stopAnimating];
	self.hidden = YES;
}

- (BOOL) isAnimating
{
	return [_indicatorView isAnimating];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
